package com.fingard.dsp.bank.directbank.bocom01;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.bocom.jump.bp.util.Base64;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ICertificateMailedMsg;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.RetAtsCertificateMailedMsg;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 
 * @author xtt
 */
public class BOCOM01CertificateMailedMsg extends BOCOM01Base implements ICertificateMailedMsg {

	@Override
	public RetAtsCertificateMailedMsg getCertificateMailedMsg(ReqCertificateMailedMsgQry p_orderMsg) throws Exception {
		RetAtsCertificateMailedMsg retAts = new RetAtsCertificateMailedMsg(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		try {
			int pageNum = 40;
			int sendNum = 0;
			int m = 0;
			if(StringHelper.isNullOrEmpty(p_orderMsg.Z_BISHU)) {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo("总笔数未填写，不允许发送交易");
				retAts.writeEnd();
				return retAts;
			}
			int total = Integer.valueOf(p_orderMsg.Z_BISHU);
			//每次推送40条
			int pageSize = total/pageNum;
			while(m < pageSize + 1) {
			BOCOM01Header bankHeader = new BOCOM01Header();
			bankHeader.tr_code = "rcvCertificationPost";
			bankHeader.sign = "3.00";
			int i = 1;
			req_no = getQryTransID();
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			// 写入通用的头部节点
			writeCommonHeader_3(xmlWriter, bankHeader);
			// 写入body
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("EXPERSS_CODE", p_orderMsg.MailNo);// 快递单号
			xmlWriter.writeElementString("EXPERSS_COMP",p_orderMsg.MailCompanyName);// 快递公司
			xmlWriter.writeElementString("DLR_FULL_NAME", p_orderMsg.DealerName);// 专营店全称
			xmlWriter.writeElementString("DLR_SHORT_NAME", "");// 专营店简称
			xmlWriter.writeElementString("DLR_CODE", p_orderMsg.DealerCode);// 专营店代码
			xmlWriter.writeElementString("MAIL_STATUS", "0");// 邮寄状态 0：在途，1：签收，2：退回。
			xmlWriter.writeElementString("MAIL_MAN", p_orderMsg.MailMan);// 寄件人
			xmlWriter.writeElementString("MAIL_TEL", p_orderMsg.MailTel);// 寄件人电话
			String mailDate = p_orderMsg.MailTime;
			if(!StringHelper.isNullOrEmpty(mailDate) && mailDate.length()>=8) {
				mailDate = mailDate.substring(0,4) + "-" + mailDate.substring(4,6) + "-" + mailDate.substring(6,8);
			}
			xmlWriter.writeElementString("MAIL_DATE",mailDate);// 邮寄时间
			xmlWriter.writeElementString("TAKE_ADDRESS", p_orderMsg.TakeAddress);// 地址
			xmlWriter.writeElementString("SUM_MAIL_NUM", p_orderMsg.MailNum);// 累计邮寄张数
			xmlWriter.writeElementString("SUM_MAIL_AMOUNT", p_orderMsg.SumMailAmount);// 累计邮寄金额
			xmlWriter.writeElementString("IS_ALL_MAIL", p_orderMsg.IsAllMail == "" ? "1" : "0");// 是否全部邮寄 0：否，1：是			
			xmlWriter.writeElementString("SILVER_TICKET_CODE","130129005067620310622001406125");// 银票号
			xmlWriter.writeStartElement("CERTIFICATION_LIST");
			while (sendNum < pageNum && p_orderMsg.readRecord()) {
				sendNum ++;
				ReqCertificateMailedMsgQryDetail tmpDetail = p_orderMsg.reqDetail;
				XElement tmpXElement = new XElement("CERTIFICATION", "");
				tmpXElement.addChild(new XElement("DATA_NO", "" + i++));
				tmpXElement.addChild(new XElement("VIN", tmpDetail.VIN));
				//tmpXElement.addChild(new XElement("PRODUCE_DATE","2019-05-23"));
				xmlWriter.writeXElement(tmpXElement);
			}
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			String reqXml = Base64.encode(xmlWriter.toXmlString().getBytes("UTF-8"));
			XmlTextWriter xmlWriters = new XmlTextWriter();// 发银行报文的XML对象
			tr_code = "FHTS01";
			writeCommonHeader(xmlWriters);
			xmlWriters.writeStartElement("body");
			xmlWriters.writeElementClosed("rem");
			xmlWriters.writeElementString("systemId", "SCF");
			xmlWriters.writeElementString("data", reqXml);
			xmlWriters.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriters.toXmlString());// 发送银行
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				// 解析银行返回
				// base64解码
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
					String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
					if ("0".equals(ans_code)) {
						String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
						String particular_info = tmpBkReader.getFirstText("/ap/head/particular_info");
						if ("0000".equals(particular_code)) {
							String body = new String(Base64.decode(tmpBkReader.getFirstText("/ap/body/RspMsgContent")));
							WriteBankLogStep3(body);
							StringBuilder bodyMsg = new StringBuilder();
							// 解析银行返回
							XmlTextReader bodyBkReader = loadBankRespXmlReader(body, bodyMsg);
							if (bodyBkReader != null) {
								String tmpRetCode = bodyBkReader.getFirstText("/mes/head/RSP_STATUS");
								tmpRespInfo = bodyBkReader.getFirstText("/Mes/head/RSP_MSG");
								if (tmpRetCode.endsWith("N")) {
									while (bodyBkReader.read("/mes/body/LIST/ITEM")) {
										String tmpReturnCode = bodyBkReader.readedXElement.getSingleTextNotNull("FLAG");
										String tmpReturnMsg = bodyBkReader.readedXElement
												.getSingleTextNotNull("MESSAGE");
										String DATA_NO = bodyBkReader.readedXElement.getSingleTextNotNull("DATA_NO");
										if (tmpReturnCode.equals("Success")) {
											tmpRespCode = "0";
											tmpRespInfo = "处理成功";
										} else {
											tmpRespCode = "-1";
											tmpRespInfo = tmpReturnCode + " : " + tmpReturnMsg;
											bodyMsg.append("序号：" + DATA_NO + "处理失败" + tmpRespInfo + ",");
										}
									}
								} else {
									tmpRespCode = "-1";
									tmpRespInfo = tmpRetCode;
								}
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = bodyMsg.toString();
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = particular_info;
						}
						sendNum = 0;
						m++;
					} else {
						retAts.writeRespCode("-1");
						retAts.writeRespInfo(ans_info);
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = tmpStrRet[1];
			}
		  }
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
